package cn.initcap.concurrency.concurrency.singleton;

import cn.initcap.concurrency.annoations.Recomend;
import cn.initcap.concurrency.annoations.ThreadSafe;

/**
 * 饿汉模式{枚举，最安全的方式}
 *
 * @author initcap
 * @date Created in 2018/6/14 PM12:26.
 */
@ThreadSafe
@Recomend
public class SingletonStaticFactory {

    /**
     * 私有构造函数
     */
    private SingletonStaticFactory() {

    }

    /**
     * 静态的工厂方法
     */
    public static SingletonStaticFactory getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonStaticFactory instance;

        /**
         * JVM保证这个方法绝对只调用一次
         */
        Singleton() {
            instance = new SingletonStaticFactory();
        }

        public SingletonStaticFactory getInstance() {
            return instance;
        }
    }

}
